#include<stdio.h>
int ZeroNumbers(int num){
    int ret = 0, tmp;
    
    while(num != 0){
        tmp = num %2;
        if(tmp == 1)
            ret++;
        num /= 2;
    }

    return ret;
}
void Sort(int* a, int i, int j) {  
    int tmp;  
    if(i == j - 1 && a[i] > a[j]){
        tmp = a[i];
        a[i] = a[j];
        a[j] = tmp;
    }
    else{
        for (int m = i; m <= j; m++) { // 注意这里是 j-1  
        for (int n = i; n <= j - m; n++) { // 修改循环条件以避免越界  
            if (a[n] > a[n + 1]) {  
                tmp = a[n];  
                a[n] = a[n + 1];  
                a[n + 1] = tmp;  
            }  
        }  
    }  
    }
   
}
 
void FindSortArea(int* a, int * arr, int sz){
    int i = 0, j = 1;
    while(j <= sz){//等于很重要
        if( j != sz && arr[j] == arr[i]){//这儿j != sz要放到前面，LeetCode跑不过
            j++;
        }
        else{
            if(i != j - 1){
                Sort(a, i, j);
            }
            i = j; j++;
        }
    }
}

int canSortArray(int* a, int sz){
    int * arr = (int*)malloc(sizeof(int) * sz);
    int i;
    for(i = 0; i < sz; i++){
        arr[i] = ZeroNumbers(a[i]);
    }
    FindSortArea(a, arr, sz);
    for(i = 1; i <sz; i++){
        if(a[i] < a[i - 1]){
            return 0;
        }
    }
    return 1;
}

int main(){
    int a[] = {52,130,132,72};
    int ret = canSortArray(a, 4);
    printf("%d", ret);
    return 0;
}